// pages/edit/edit.js
const db = wx.cloud.database();
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid: '',
    listCH: 300,
    book_id: '',
    listItems: [],
    currentAudioUrl: '',
    currentAudioStatus: ''
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    var that = this;
    this.data.book_id = options._id;
    this.setData({
      openid: await getApp().getOpenid(),
      currentAudioUrl: app.currentAudioUrl,
      currentAudioStatus: app.currentAudioStatus
    });
    this.getSystemInfo();
    this.initListItems();
    app.backgroundAudioManager.onEnded(function(){ 
      if(app.currentAudioIndex < app.currentAudioList.length - 1){ 
        app.currentAudioIndex++;
        var currentAudioUrl = app.currentAudioList[app.currentAudioIndex];
        app.backgroundAudioManager.src = currentAudioUrl;
        that.setData({
          currentAudioUrl:currentAudioUrl
        });
      }
    });
  },

  gotocontent: function (e) {
    var _id = e.currentTarget.id;
    var type = e.currentTarget.dataset.type;
    if (type == "txt") {
      wx.navigateTo({
        url: '/pages/contentdetail/contentdetail?item_id=' + _id
      })
    } else {
      var idx = e.currentTarget.dataset.idx;
      var url = this.data.listItems[idx].list_url;
      if(url == this.data.currentAudioUrl){
        if(this.data.currentAudioStatus == "playing"){
          app.backgroundAudioManager.pause();
          this.setData({
            currentAudioStatus:'pause'
          });
          app.currentAudioStatus = 'pause';
        }else{
          app.backgroundAudioManager.play();
          this.setData({
            currentAudioStatus:'playing'
          });
          app.currentAudioStatus = 'playing';
        }
      }else{ 
        app.backgroundAudioManager.src = url;
        app.backgroundAudioManager.title = '网盘资料共享';
        app.backgroundAudioManager.epname = '网盘资料共享';
        this.setData({
          currentAudioUrl:url,
          currentAudioStatus:'playing'
        });
        app.currentAudioUrl = url;
        app.currentAudioStatus = 'playing'; 
        var currentAudioList = [];
        for(var i = 0; i < this.data.listItems.length;i++){
          currentAudioList.push(this.data.listItems[i].list_url);
        }
        app.currentAudioList = currentAudioList;
        app.currentAudioIndex = idx;
      }
      
    }
  },
  initListItems: function () {
    wx.showLoading({
      title: '加载中'
    })
    var that = this;
    var book_id = that.data.book_id;
    console.log(book_id);
    wx.cloud.callFunction({
      name: 'list_items_by_book_id',
      data: {
        book_id: book_id
      },
      complete: res => {
        var list_items = res.result.data;
        that.setData({
          listItems: list_items
        });
        wx.hideLoading();
      },
      error: function (e) {
        console.log(e);
      }
    })
  },
  getSystemInfo: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          listCH: res.windowHeight - 40
        })
      }
    })
  },
  del: function () {
    let that = this;
    if (that.data.book_id) {
      db.collection('books').doc(that.data.book_id).remove({
        success: function (res) {
          wx.showToast({
            title: '删除成功'
          })
          //刷新首页并返回
          that.goHome();
        }
      })
    } else {
      wx.showToast({
        title: '没有id'
      })
    }
  },
  edit: function () {
    wx.navigateTo({
      url: '/pages/edit/edit?_id=' + this.data.book_id
    })
  },
  editSubTitle: function () {
    wx.navigateTo({
      url: '/pages/listedit/listedit?book_id=' + this.data.book_id
    })
  }
})